/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * buscarMedicamento2.java
 *
 * Created on 01-may-2011, 11:00:20
 */

package interfaces.farmacia;

import Controlador.Controlador;
import GestorFarmacia.Medicamento;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import clinica.Interfaz;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Ana Ferreira Blanco
 */
public class buscarMedicamento2 extends javax.swing.JPanel {
    private ClinicaView app;
    private Interfaces anterior;
    private Medicamento[] lista=null;
    private Medicamento busqueda;

    /** Creates new form buscarMedicamento2 */
    public buscarMedicamento2(ClinicaView a) {
        app =a;
        initComponents();
    }

    private void limpiarTabla() {
        while(((DefaultTableModel)tabla_medicamentos.getModel()).getRowCount()>0)
            ((DefaultTableModel)tabla_medicamentos.getModel()).removeRow(0);

    }

    public void rellenaDatos( Medicamento m, Interfaces ant) throws SQLException, IOException, ClassNotFoundException, ParseException {
        //Limpiar consulta anterior
        limpiarTabla();
        busqueda=m;
        anterior = ant;
        tabla_medicamentos.setRowHeight(32);
        tabla_medicamentos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lista = Controlador.buscarMedicamento(null,m.getCategoriaMedicamento(), m.getNombreMedicamento(),m.getDCI(),m.getViaAdministracion(),null, null, null, null);

        if (lista == null)
            app.abrirMensajeError("No se encontró ningún medicamento.", anterior);
        else {
            for(Medicamento l: lista) {
            //Valores a mostrar en la tabla
            String [] valores = {""+l.getNombreMedicamento(),""+l.getCategoriaMedicamento(),""+l.getDCI(),""+l.getViaAdministracion()};
            ((DefaultTableModel)tabla_medicamentos.getModel()).addRow(valores);
            }
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_medicamentos = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 470));
        setPreferredSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Medicamentos encontrados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jPanel1.setMinimumSize(new java.awt.Dimension(780, 470));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(452, 402));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Acciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(buscarMedicamento2.class, this);
        jButton5.setAction(actionMap.get("verMedicamento")); // NOI18N
        jButton5.setFont(Interfaz.setFontSize());
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(buscarMedicamento2.class);
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText("Ver");
        jButton5.setLabel(resourceMap.getString("jButton5.label")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel2.add(jButton5);

        jButton7.setAction(actionMap.get("modificarMedicamento")); // NOI18N
        jButton7.setFont(Interfaz.setFontSize());
        jButton7.setIcon(resourceMap.getIcon("jButton7.icon")); // NOI18N
        jButton7.setText("Modificar");
        jButton7.setLabel(resourceMap.getString("jButton7.label")); // NOI18N
        jButton7.setName("jButton7"); // NOI18N
        jButton7.setPreferredSize(new java.awt.Dimension(0, 0));
        jButton7.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jButton7);

        jButton8.setAction(actionMap.get("verLote")); // NOI18N
        jButton8.setFont(Interfaz.setFontSize());
        jButton8.setIcon(resourceMap.getIcon("jButton8.icon")); // NOI18N
        jButton8.setText("Consultar");
        jButton8.setLabel(resourceMap.getString("jButton8.label")); // NOI18N
        jButton8.setName("jButton8"); // NOI18N
        jButton8.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel2.add(jButton8);

        jButton6.setAction(actionMap.get("aniadirLote")); // NOI18N
        jButton6.setFont(Interfaz.setFontSize());
        jButton6.setIcon(resourceMap.getIcon("jButton6.icon")); // NOI18N
        jButton6.setText("Añadir");
        jButton6.setLabel(resourceMap.getString("jButton6.label")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel2.add(jButton6);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla_medicamentos.setFont(new java.awt.Font("Tahoma", 0, 18));
        tabla_medicamentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Categoría", "DCI o marca", "Vía Administración"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla_medicamentos.setName("tabla_medicamentos"); // NOI18N
        tabla_medicamentos.setUpdateSelectionOnSort(false);
        jScrollPane1.setViewportView(tabla_medicamentos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setLabel(resourceMap.getString("jButton3.label")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 760, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 366, Short.MAX_VALUE)
                .addGap(9, 9, 9)
                .addComponent(jButton3)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    @Action
    public void verMedicamento() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {
        int row = tabla_medicamentos.getSelectedRow();
        if ( row < 0 ) {
            app.abrirMensajeError("No se seleccionó ningún medicamento.", Interfaces.BUSCAR_MEDICAMENTO_2);
        } else {
            Medicamento m = lista[row];
            app.abrirAltaYModMedicamento3(m, Interfaces.BUSCAR_MEDICAMENTO_2);
        }
    }
     @Action
    public void modificarMedicamento() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {
        int row = tabla_medicamentos.getSelectedRow();
        if ( row < 0 ) 
            app.abrirMensajeError("No se seleccionó ningún medicamento.", Interfaces.BUSCAR_MEDICAMENTO_2);
        else{
         Medicamento m = lista[tabla_medicamentos.getSelectedRow()];
        app.abrirAltaYModMedicamento2(m, Interfaces.BUSCAR_MEDICAMENTO_2, busqueda);
         }
    }
      @Action
    public void aniadirLote() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {

        int row = tabla_medicamentos.getSelectedRow();
        if ( row < 0 )
            app.abrirMensajeError("No se seleccionó ningún medicamento.", Interfaces.BUSCAR_MEDICAMENTO_2);
        else{
            Medicamento m = lista[tabla_medicamentos.getSelectedRow()];
            app.abrirAltaLote(m, Interfaces.BUSCAR_MEDICAMENTO_2);
          }
    }
       @Action
    public void verLote() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {
        int row = tabla_medicamentos.getSelectedRow();
        if ( row < 0 ) 
            app.abrirMensajeError("No se seleccionó ningún medicamento.", Interfaces.BUSCAR_MEDICAMENTO_2);
        else{
        Medicamento m = lista[tabla_medicamentos.getSelectedRow()];
        app.abrirConsultarLote(m.getIdMedicamento(), Interfaces.BUSCAR_MEDICAMENTO_2);
        }
    }
    @Action
    public void cancelar() {
        app.volver(anterior);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla_medicamentos;
    // End of variables declaration//GEN-END:variables

}
